<%@ taglib uri="/tags/struts-bean" prefix="bean"%>
<%@ taglib uri="/tags/struts-html" prefix="html"%>
<%@ taglib uri="/tags/struts-logic" prefix="logic"%>
<script type="text/javascript">
	function leaveThisForm(forma, customerId, customerDescription, materialId, materialDescription){
		forma.customerId.value = customerId;
		forma.customerDescription.value = customerDescription;
		forma.materialId.value = materialId;
		forma.year.value = document.getElementById('fiscalYear').value;
		forma.submit();
	}

	function validateForm(action, forma,showError){
		var cliente = document.getElementById('customer').value;
		var producto = document.getElementById('material').value;
		var clienteDesc = document.getElementById('customerDescriptionForm').value;
		var productoDesc = document.getElementById('materialDescriptionForm').value;
		if( cliente != "" && producto != "" && clienteDesc != "" && productoDesc != "" ){
			forma.action = action;
			forma.submit();
			return true;
		}else{
			if( showError ){
				alert("Error : \n<bean:message key="label.forecast.error"/>!");
			}
			return false;
		}
	}

	function sendRequest(url, form, field, fieldDescription, maxlength){
		var customerDescription = "";
		if( trim( field.value ).length == maxlength ){
			form.id.value = trim( field.value );
			retrieveURL(url, form.name);
		}else{
			fieldDescription.value = "";
		}
	}
</script>
<html:form action="/goCustomerSalesForecast.do">
	<input type="hidden" name="customerId"/>
	<input type="hidden" name="customerDescription"/>
	<input type="hidden" name="materialId"/>
	<input type="hidden" name="materialDescription"/>
	<input type="hidden" name="year"/>
</html:form>
<html:form action="/goProductSalesForecast.do">
	<input type="hidden" name="customerId"/>
	<input type="hidden" name="customerDescription"/>
	<input type="hidden" name="materialId"/>
	<input type="hidden" name="materialDescription"/>
	<input type="hidden" name="year"/>
</html:form>
<html:form action="/searchCustomerForecast">
	<input type="hidden" value="" name="id"/>
</html:form>
<html:form action="/searchMaterialForecast">
	<input type="hidden" value="" name="id"/>
</html:form>
<html:form action="/saveSalesForecast.do" styleId="mainForm">
	<input type="hidden"" name="search" value="Y"/>
	<table align="center" width="50%" cellpadding="2" cellspacing="1" border="0" bgcolor="#CCCCC0">
		<tr>
			<th colspan="2" align="center" class="titulocolumna"><bean:message key="menu.module.corn.instructionpurchase.buy"/></th>			 
		</tr>
		<tr>
			<td class="etiqueta"><bean:message key="message.futurepurchase.customer"/>: </td>
			<td class="etiqueta_campo" style="text-align: left;">
				<html:text name="SalesForecastForm" property="salesForecastView.customer" styleId="customer" maxlength="6" size="5"  styleClass="campo_texto" onkeydown="return (numeric(event, 0) && noPaste(event, this));" onkeyup="extractNumber(this,2,false);" onfocus="this.select();"  onchange="sendRequest('searchCustomerForecast.do?x=1', document.SearchCustomerForecastForm, this, this.form.customerDescriptionForm, 6);"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -
				<span id="customerDescriptionAjax"> 
					<html:text name="SalesForecastForm" property="salesForecastView.customerDescription" styleId="customerDescriptionForm" maxlength="3" size="50"  styleClass="campo_texto" onfocus="this.select();" readonly="true"/>
				</span>
				<html:button property="GoCustomerSelection" value="..." styleClass="boton_short" onclick="leaveThisForm(SalesForecastFormCustomerSelection, document.getElementById('customer').value, document.getElementById('customerDescriptionForm').value, document.getElementById('material').value, document.getElementById('materialDescriptionForm').value)" />
			</td>
		</tr>
		<tr>
			<td class="etiqueta"><bean:message key="message.futurepurchase.product"/>: </td>
			<td class="etiqueta_campo" style="text-align: left;">
				<html:text name="SalesForecastForm" property="salesForecastView.material" styleId="material" maxlength="10" size="8"  styleClass="campo_texto" onfocus="this.select();"  onchange="sendRequest('searchMaterialForecast.do?x=1', document.SearchMaterialForecastForm,  this, this.form.materialDescriptionForm, 10);"/> -	
				<span id="materialDescriptionAjax">
					<html:text name="SalesForecastForm" property="salesForecastView.materialDescription" styleId="materialDescriptionForm" maxlength="3" size="50"  styleClass="campo_texto" onfocus="this.select();" readonly="true"/>
				</span>
				<html:button property="GoMaterialSelection" value="..." styleClass="boton_short" onclick="leaveThisForm(SalesForecastFormProductSelection, document.getElementById('customer').value, document.getElementById('customerDescriptionForm').value, document.getElementById('material').value, document.getElementById('materialDescriptionForm').value)" />
			</td>
		</tr>
		<tr>
			<td class="etiqueta"><bean:message key="label.forecast.year"/>: </td>
			<td class="etiqueta_campo" style="text-align: left;"> 
			  <html:select name="SalesForecastForm" property="salesForecastView.fiscalYear" styleId="fiscalYear" styleClass="campo_texto" onchange="validateForm('addSalesForecast.do', this.form, false);">
			  	<logic:notEmpty name="years">
					<html:options collection="years" property="value" labelProperty="label" />
				</logic:notEmpty>
			  </html:select> 
			</td>
		</tr>
		<tr>
			<td class="etiqueta_campo" style="text-align: center;" colspan="2"> 
			   <html:button styleClass="boton" property="searchButton" onclick="validateForm('addSalesForecast.do', this.form, true)"><bean:message key="label.common.query"/></html:button>
			</td>
		</tr>
	</table>
	<br/>
	<span id="periodForecast">
		<logic:notEmpty name="SalesForecastForm" property="salesForecastViews">
			<table align="center" width="95%" cellpadding="2" cellspacing="1" border="0" bgcolor="#CCCCC0">
				<tr>
					<logic:iterate id="meses" name="SalesForecastForm" property="salesForecastViews">
						<td class="etiqueta_center" align="center"><bean:write name="meses"  property="month"/> <bean:write name="meses"  property="fiscalYear"/></td>
					</logic:iterate>
				</tr>
				<tr>
					<logic:iterate id="pronostico" name="SalesForecastForm" property="salesForecastViews">
						<td class="etiqueta_campo_center" align="center">
							<bean:define id="contractedValue"><bean:write name="pronostico"  property="contracted"/></bean:define>
							<logic:equal name="pronostico"  property="allowMod" value="1">
								<html:text name="salesForecastViews" property="contracted" indexed="true" value="<%=contractedValue%>"  styleClass="campo_texto_r" size="10"  onfocus="this.select();" onkeydown="return (numeric(event, 1) && noPaste(event, this));" onkeyup="extractNumber(this,4,false);"/>
	 						</logic:equal>
	 						<logic:equal name="pronostico"  property="allowMod" value="0">
	 							<html:text disabled="true" name="salesForecastViews" property="contracted" indexed="true" value="<%=contractedValue%>"  styleClass="campo_texto_r" size="10"  onfocus="this.select();" onkeydown="return (numeric(event, 1) && noPaste(event, this));" onkeyup="extractNumber(this,4,false);"/>
	 						</logic:equal>
						</td>
					</logic:iterate>
				</tr>
				<tr>
					<td class="etiqueta_campo" style="text-align: center;" colspan="12"> 
					   <html:button styleClass="boton" property="saveButton" onclick="validateForm('saveSalesForecast.do', this.form, true);"><bean:message key="label.common.save"/></html:button>
					</td>
				</tr>
			</table>
		</logic:notEmpty>
	</span>
</html:form>